const App = getApp();

Component({
  properties: {
    showMakes: Boolean,
    name: String,
    cookId: String,
    roomId: String,
    page: String
  },
  data: {
    showTime: true,
    showSucTips: true,
    region: [],
  },
  methods:{
    showTime(e) {
      this.setData({
        showTime: !this.data.showTime
      })
    },
    //关闭预约窗口
    closeMake() {
      this.setData({
        showMakes: false
      })
    },
    //获取子组件传递过来的time
    getDate(e){
      let time = e.detail;
      this.setData({
        time: time,
        showTime: true
      })
    },
    //提交预约信息
    submit(e){
      let name = e.detail.value.name.replace(/\s+/g, '');
      let phone = e.detail.value.phone.replace(/\s+/g, '');
      let member = e.detail.value.member.replace(/\s+/g, '');
      let time = this.data.time;
      let nameReg = /^[\u4E00-\u9FA5A-Za-z]+$/;
      let page = this.data.page;
      let region = this.data.region;
      let serviceAddress;
      let id;
      let _this = this;

      if (page == 'makeroom'){
        id = this.data.roomId
      }else{
        id = this.data.cookId
      }
      
      if (!name) {
        wx.showToast({
          title: '请填写姓名',
          icon: 'none'
        })
        return
      }

      if (!nameReg.test(name)){
        wx.showToast({
          title: '姓名只能输入中文和英文',
          icon: 'none'
        })
        return
      }

      if (!phone) {
        wx.showToast({
          title: '请填写电话',
          icon: 'none'
        })
        return
      }

      if (!(/^1[345789]\d{9}$/.test(phone))) {
        wx.showToast({
          title: '电话填写错误',
          icon: 'none'
        })
        return
      }

      if (!member) {
        wx.showToast({
          title: '请填写用餐人数',
          icon: 'none'
        })
        return
      }

      if (!(/^[1-9]\d*$/).test(member)){
        wx.showToast({
          title: '用餐人数填写错误',
          icon: 'none'
        })
        return
      }

      if(!time){
        wx.showToast({
          title: '请选择预约时间',
          icon: 'none'
        })
        return
      }

      if (page == 'makechef') {
        
        serviceAddress = e.detail.value.serviceAddress.replace(/\s+/g, '');

        if (!this.data.region.length) {
          wx.showToast({
            title: '请选择服务地址',
            icon: 'none'
          })
          return
        }

        if (!serviceAddress) {
          wx.showToast({
            title: '请填写详细地址',
            icon: 'none'
          })
          return
        }

        if (serviceAddress.length < 5) {
          wx.showToast({
            title: '详细地址需输入5字以上',
            icon: 'none'
          })
          return
        }

        serviceAddress = region[0] + "-" + region[1] + "-" + region[2] + " " + serviceAddress
      }

      time = new Date().getFullYear() + "-" + time

      if (page == 'makeroom') {
        App._post_form('room/book', {
          book_name: name,
          book_mobile: phone,
          book_time: time,
          member,
          room_id: id  
        }, function (result) {
          if(result.code ==1){
            _this.setData({
              showMakes: false,
              showSucTips: false
            })
          }else{
            App.showSuccess(result.msg);
          }
        });
      } else {
        App._post_form('cook/book', {
          book_name: name,
          book_mobile: phone,
          book_time: time,
          member,
          cook_id: id,
          service_address: serviceAddress
        }, function (result) {
          if (result.code == 1) {
            _this.setData({
              showMakes: false,
              showSucTips: false
            })
          } else {
            App.showSuccess(result.msg);
          }
        });
      }
    },
    redirectHome(){
      wx.switchTab({
        url: '/pages/index/index',
      })
    },
    closeSucTips(){
      this.setData({
        showSucTips: true
      })
    },
    bindRegionChange(e) {
      this.setData({
        region: e.detail.value
      })
    }
  }
})